package com.iotechn.unimall.admin.api.cardOrder;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.core.util.DateUtils;
import com.iotechn.unimall.data.domain.CardOrderDO;
import com.iotechn.unimall.data.domain.CardOrderDetailDO;
import com.iotechn.unimall.data.dto.userCard.AdminCardOrderDetailDTO;
import com.iotechn.unimall.data.mapper.CardOrderDetailMapper;
import com.iotechn.unimall.data.mapper.CardOrderMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.*;


@Service
public class AdminCardOrderServiceImpl implements AdminCardOrderService {
    @Autowired
    private CardOrderMapper cardOrderMapper;

    @Autowired
    private CardOrderDetailMapper cardOrderDetailMapper;

    @Override
    public Page<CardOrderDO> list(String phone, String cardNo, String cardOrderNo, Integer status, Long storeId, String startTimeStr, String endTimeStr, Integer page, Integer limit, Long adminId) throws ServiceException {
        Date startTime = null;
        Date endTime = null;
        try {
            if (StringUtils.isNotEmpty(startTimeStr)) {
                startTime = DateUtils.getDateByStr(startTimeStr, "yyyy-MM-dd hh:mm:dd");
            }
            if (StringUtils.isNotEmpty(endTimeStr)) {
                endTime = DateUtils.getDateByStr(endTimeStr, "yyyy-MM-dd hh:mm:dd");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Set<Long> orderIds = new HashSet<>();
        int currPage = (page - 1) * limit;
        int pageSize = limit;
        List<CardOrderDO> cardOrderDOS = cardOrderMapper.selectOrderDetailList(phone, cardNo, cardOrderNo, status, storeId, startTime, endTime, currPage, pageSize);
        Integer count = cardOrderMapper.selectOrderDetailCount(phone, cardNo, cardOrderNo, status, storeId, startTime, endTime);
        return new Page<>(cardOrderDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CardOrderDO create(CardOrderDO cardOrderDO, Long adminId) throws ServiceException {
        Date now = new Date();
        cardOrderDO.setGmtCreate(now);
        cardOrderDO.setGmtUpdate(now);
        if (cardOrderMapper.insert(cardOrderDO) > 0) {
            cardOrderDO.setId(cardOrderDO.getId());
            return cardOrderDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(CardOrderDO cardOrderDO, Long adminId) throws ServiceException {
        Long id = cardOrderDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        cardOrderDO.setGmtUpdate(now);
        if (cardOrderMapper.updateById(cardOrderDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (cardOrderMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public CardOrderDO detail(Long id, Long adminId) throws ServiceException {
        return cardOrderMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        CardOrderDO cardOrderDO = new CardOrderDO();
        cardOrderDO.setId(id);
        cardOrderDO.setStatus(status);
        cardOrderDO.setGmtUpdate(new Date());
        if (cardOrderMapper.updateById(cardOrderDO) > 0) {
            return true;
        }
        return false;
    }


    @Override
    public AdminCardOrderDetailDTO cardOrderDetail(Long orderId, Long adminId) throws ServiceException {
        AdminCardOrderDetailDTO orderDetailDTO = new AdminCardOrderDetailDTO();
        CardOrderDO orderDO = cardOrderMapper.selectById(orderId);
        BeanUtils.copyProperties(orderDO, orderDetailDTO);
        Wrapper<CardOrderDetailDO> cetailWrapper = new EntityWrapper<>();
        cetailWrapper.eq("card_order_id", orderId);
        List<CardOrderDetailDO> cardOrderDetailDOS = cardOrderDetailMapper.selectList(cetailWrapper);
        orderDetailDTO.setCardDetails(cardOrderDetailDOS);
        return orderDetailDTO;
    }
}